package com.py.framework.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.repository.Model;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.py.framework.common.Constants;
import com.py.framework.common.controller.SystemController;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.core.log.ExceptionLogger;
import com.py.framework.core.rest.support.RestServiceResult;
import com.py.framework.service.ActManageService;

/**
 * 工作流管理Controller
 * @author Leegern
 * @date   2018年5月22日
 */
@RestController
@RequestMapping("/act")
public class ActManageController extends SystemController {
	
	@Autowired
	private ActManageService actManageService;
	

	/**
	 * @api {post} /act/queryActDataList.json 分页查询工作流程列表
	 * @apiName queryActDataList
	 * @apiVersion 0.0.1
	 * @apiGroup 工作流管理
	 * @apiParam {int} page  当前页码
	 * @apiParam {int} limit 每页条数
	 * @apiParam {String} actName 流程名称
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {int} id 序号
	 * @apiSuccess {String} name 名称
	 * @apiSuccess {String} key  键值
	 * @apiSuccess {String} version 版本号
	 * @apiSuccess {String} metaInfo 元数据
	 * @apiSuccess {String} lastUpdateTime 最后更新时间
	 * @apiSuccess {String} createTime 创建时间
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":2,"data":[{"createTime":1505379688112,"id":"410086","version":"v_1.40","key":"application","lastUpdateTime":1505379899718,"metaInfo":"{\"name\":\"在线提交核查报告\",\"revision\":1,\"description\":\"在线申请核查报告\"}"]}
	 */
	@RequestMapping(value = "/queryActDataList", method = RequestMethod.POST)
	public RestServiceResult<List<Model>> queryActDataList(int page, int limit, String actName) {
		Page pager = super.getPage(page, limit);
		List<Model> datas = actManageService.queryActDataList(pager, actName);
		return super.getPageSuccessRestServiceResult(pager.getTotalCount(), datas);
	}
	
	/**
	 * @api {post} /act/createAct.json 创建工作流程(流程编辑器请求路径/service/editor?id=111)
	 * @apiName createAct
	 * @apiVersion 0.0.1
	 * @apiGroup 工作流管理
	 * @apiParam {String} actName 流程名称
	 * @apiParam {String} actKey 流程键
	 * @apiParam {String} actDesc 流程描述
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {int} modelId
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":2,"data":{"modelId": 10000}}
	 */
	@RequestMapping(value = "/createAct", method = RequestMethod.POST)
	public RestServiceResult<Object> createAct(String actName, String actKey, String actDesc) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		Map<String, Object> data = new HashMap<String, Object>();
		String modelId = "";
		if (StringUtils.isNotBlank(actName) && StringUtils.isNotBlank(actKey)) {
			try {
				modelId = actManageService.createAct(actName, actKey, actDesc);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("创建失败！");
				ExceptionLogger.error(e);
			}
		}
		data.put("modelId", modelId);
		result.setData(data);
		return result;
	}

	/**
	 * @api {post} /act/deployAct.json 部署工作流程
	 * @apiName deployAct
	 * @apiVersion 0.0.1
	 * @apiGroup  工作流管理
	 * @apiParam {String} modelId 模型Id
	 * @apiParamExample {json} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} result
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/deployAct", method = RequestMethod.POST)
	public RestServiceResult<Object> deployAct(String modelId) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (StringUtils.isNotBlank(modelId)) {
			try {
				actManageService.deployAct(modelId);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("部署失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /act/batchDelAct.json 批量删除流程
	 * @apiName batchDelAct
	 * @apiVersion 0.0.1
	 * @apiGroup 工作流管理
	 * @apiParam {String} ids 模型Id集合
	 * @apiParamExample {String} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccessExample {json} 成功返回结果
	 *   {"code":0,"msg":null,"count":0,"data":null}
	 */
	@RequestMapping(value = "/batchDelAct", method = RequestMethod.POST)
	public RestServiceResult<Object> batchDelAct(String ids) {
		RestServiceResult<Object> result = super.getSuccessRestServiceResult();
		if (StringUtils.isNotBlank(ids)) {
			try {
				actManageService.deleteAct(Arrays.asList((ids.split(","))));
			} catch (ServiceException e) {
				result = super.getFailedRestServiceResult(e.getMessage());
				ExceptionLogger.error(e);
			} catch (Exception e) {
				result = super.getFailedRestServiceResult("删除失败！");
				ExceptionLogger.error(e);
			}
		}
		return result;
	}
	
	/**
	 * @api {post} /act/isExistAct.json 根据流程名称判断是否有重复数据
	 * @apiName isExistAct
	 * @apiVersion 0.0.1
	 * @apiGroup 工作流管理
	 * @apiParam {String} actName 流程名称
	 * @apiParamExample {String} 请求参数示例
	 *  {}
	 * @apiSuccess {String} code 状态码:0 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} true 存在重复, false 不存在重复
	 * @apiSuccessExample {json} 成功返回结果
	 *    {"code":0,"msg":null,"count":0,"data":{"flag":"true"}}
	 */
	@RequestMapping(value = "/isExistAct", method = RequestMethod.POST)
	public RestServiceResult<Map<String, String>> isExistAct(String actName) {
		RestServiceResult<Map<String, String>> rstData = null;
		Map<String, String> result = new HashMap<>();
		String flag = "";
		if (StringUtils.isNotBlank(actName)) {
			try {
				int count = actManageService.getActsByName(actName);
				if (count > Constants.NUMBER_ZERO) {
					flag = "true";
				} else {
					flag = "false";
				}
				result.put("flag", flag);
			} catch (Exception e) {
				rstData = super.getFailedRestServiceResult("查询失败！");
				ExceptionLogger.error(e);
			}
		}
		rstData = super.getSuccessRestServiceResult(result);
		return rstData;
	}
}